package com.jieao.front.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jieao.biz.domain.SiteFeedBack;
import com.jieao.biz.domain.SiteNotice;
import com.jieao.biz.dto.SiteFeedBackDto;
import com.jieao.common.core.controller.BaseController;
import com.jieao.common.core.domain.AjaxResult;
import com.jieao.common.core.page.TableDataInfo;
import com.jieao.content.domain.ArticleComment;
import com.jieao.content.dto.ArticleCommentDto;
import com.jieao.content.dto.ArticleDto;
import com.jieao.content.service.IArticleCommentService;
import com.jieao.content.service.IArticleService;
import com.jieao.framework.util.ShiroUtils;
import com.jieao.site.service.ISiteFeedBackService;
import com.jieao.site.service.ISiteNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/front/index")
public class FrontIndexController extends BaseController {
    @Autowired
    private IArticleService articleService;
    @Autowired
    private ISiteNoticeService siteNoticeService;
    @Autowired
    private IArticleCommentService articleCommentService;
    @Autowired
    private ISiteFeedBackService siteFeedBackService;
    @GetMapping()
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "front/index";
    }

    /**
     * 分页获得文章
     * @param
     * @return {com.jieao.common.core.page.TableDataInfo}
     * @author LWF
     * @date 2020/7/12 13:16
     **/
    @PostMapping(value = "/articles", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public TableDataInfo articles() {
        startPage();
        ArticleDto article = new ArticleDto();
        article.setIsTop("Y");
        List<ArticleDto> list = articleService.selectFrontArticleList(article);
        return getDataTable(list);
    }

    /**
     * 获取首页通知
     * @Author LWF
     * @Date 13:16 2020/7/12
     **/
    @GetMapping(value = "/notice")
    @ResponseBody
    public AjaxResult notice() {
        List<SiteNotice> list = siteNoticeService.list(Wrappers.<SiteNotice>query().lambda()
                .eq(SiteNotice::getStatus, 1)
                .eq(SiteNotice::getType, 2));
        if (CollectionUtils.isNotEmpty(list)) {
            Collections.shuffle(list);
            return AjaxResult.success(list.get(0));
        } else {
            return AjaxResult.error("未查询到通知");
        }
    }

    /**
     * 获取首页评论
     * @Author LWF
     * @Date 13:16 2020/7/12
     **/
    @PostMapping(value = "/newComments", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public TableDataInfo newComment() {
        startPage();
        List<ArticleCommentDto> listComment = articleCommentService.pageCommentList(new ArticleComment());
        return getDataTable(listComment);
    }

    /**
     * 获取首页反馈
     * @Author LWF
     * @Date 13:16 2020/7/12
     **/
    @PostMapping(value = "/newFeedbacks", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public TableDataInfo feedbacks() {
        startPage();
        List<SiteFeedBackDto> siteFeedBackDtos = siteFeedBackService.selectSiteFeedBackList(new SiteFeedBackDto());
        return getDataTable(siteFeedBackDtos);
    }

    /**
     * 发送反馈
     * @param siteFeedBack
     * @return {java.lang.String}
     * @author LWF
     * @date 2020/7/12 23:20
     **/
    @PostMapping(value = "/sendFeedback", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public AjaxResult pushComment(@RequestBody SiteFeedBack siteFeedBack) {
        siteFeedBack.setUserId(ShiroUtils.getUserId());
        siteFeedBackService.insertSiteFeedBack(siteFeedBack);
        return AjaxResult.success("发送成功");
    }
}
